<?php
/**
 * 插件类
 */

include_once FUN_PTH.'plugin.inc.php';

class Plugin extends MyBase{
	
	function __construct() {
		parent::__construct('plugin.id', array('keyword' => array('name', 'desc')));
	}
	
	/**
	 * 修改插件
	 */
	public function edit($post){
		$id = empty($post['id']) ? 0 : intval($post['id']);
		if($id <= 0)return false;
		$select = array('kept' => " AND id!={$id}");
		if(!empty($post['mark'])){
			$select['mark'] = $post['mark'];
		}else{
			$select['name'] = $post['name'];			
		}
		if($id > 0){
			$select['kept'] = " AND id!={$id}";
		}
		$rs = $this->db->get_row($this->tbname, $select, 'id');
		if(!empty($rs)){return lang('already').lang('exist');}
		
		$data = array (
			'name'			=> $post['name'],
			'url'			=> $post['url'],
			'author'		=> $post['author'],
			'version'		=> $post['version'],
			'pubdate'		=> $post['date'],
			'desc'			=> $post['desc'],
		);
		if($id > 0){
			$data['updatetime'] = TIME;
			$r = $this->db->update($this->tbname, array('id' => $id), $data);
		}else{
			$data['addtime'] = TIME;
			$r = $id = $this->db->insert($this->tbname, $data);
		}
		if($r > 0){
			return $this->cache_plugin(); // 缓存插件
		}
		return $id;
	}
	
	public function update(){
		$plugins = get_plugins();
		foreach($plugins as $mark => $plugin){
			if(!is_array($plugin))continue;
			$data = array (
				'mark'	=> $mark,
				'name'			=> $plugin['name'],
				'url'			=> $plugin['url'],
				'author'		=> $plugin['author'],
				'version'		=> $plugin['version'],
				'pubdate'		=> $plugin['date'],
				'desc'			=> $plugin['desc'],
				'preview'		=> $plugin['preview'],
				'plugin_file'	=> $plugin['plugin_file'],
			);
			$rs = $this->get_row_by_mark($mark, '`id`');
			if(!empty($rs)){
				$data['updatetime'] = TIME;
				// $r = $this->db->update($this->tbname, array('id' => $rs['id']), $data);
			}else{
				$data['addtime'] = TIME;
				$r = $id = $this->db->insert($this->tbname, $data);
			}
		}
		return $this->cache_plugin(); // 缓存插件
	}
		
	/**
	 * 更新排序
	 */
	public function rank($post){
		if(!is_array($post['rank']))$post['rank'] = array($post['rank']);
		$iRtn = 0;
		foreach($post['lst'] as $id){
			$data = array(
				'rank'		 => $post['rank'][$id], 
				'updatetime' => TIME 
			);
			if($this->db->update($this->tbname, array('id' => $id), $data)){
				$iRtn ++;
			}
		}
		return $this->cache_plugin();
	}
	
	public function activate($post){
		if(!is_array($post['state']))$post['state'] = array($post['state']);
		$GLOBALS['plugin_funcs'] = array();	// 放在这里有些不好理解，原在plugin.inc.php里面
		$iRtn = 0;
		foreach($post['lst'] as $id){
			if(isset($post['state'][$id])){
				$state = 1;
			}else{
				$state = 0;
			}
			$plugin = $this->get_row($id);
			$funcs = activate_plugin($plugin['mark'], $state);
			if($funcs){	// 处理成功
				$filters = isset($funcs['filters']) && is_array($funcs['filters']) ? $funcs['filters'] : array();
				$actions = isset($funcs['actions']) && is_array($funcs['actions']) ? $funcs['actions'] : array();
				$data = array(
					'state'		 => $state, 
					'filters'	=> json_encode($filters),
					'actions'	=> json_encode($actions),
					'updatetime' => TIME,
				);
				if($this->db->update($this->tbname, array('id' => $id), $data)){
					$iRtn ++;
				}
			}
		}
		cache_write('plugins.php', $GLOBALS['plugin_funcs']);	// 放在这里有些不好理解，原在plugin.inc.php里面
		return $this->cache_plugin();
	}
			
	
	/**
	 * 取一条记录
	 */
	/*public function get_row($id, $fields = '*'){
		return $this->db->get_row($this->tbname, array('id' => $id), $fields);
	}*/
	
	public function get_row_by_mark($mark, $fields = '*'){
		return $this->db->get_row($this->tbname, array('mark' => $mark), $fields);
	}
	
	/**
	 * 取列表
	 */
	/*public function get_list($fields = '*', $select = array(), $order = array(), $page = 1, $size = 10, $offset = PAGE_OFFSET){
		$select['kept'] = '';
		if(!empty($select['keyword'])){
			$select['kept'] .= " AND (`name` LIKE '%{$select['keyword']}%' OR `desc` LIKE '%{$select['keyword']}%')";
		}
		return $this->db->get_list($this->tbname, $fields, $select, $order, $page, $size, $offset);
	}*/
	
	/*public function get_all($fields = '*', $select = array(), $order = array()){
		$select['kept'] = '';
		if(!empty($select['keyword'])){
			$select['kept'] .= " AND (`name` LIKE '%{$select['keyword']}%' OR `desc` LIKE '%{$select['keyword']}%')";
		}
		return $this->db->get_all($this->tbname, $fields, $select, $order);
	}*/
	
	/********************************************************************************
	 * 缓存
	 ********************************************************************************/
		
	public function cache_plugin(){
		$plugin_all = array();
		$plugins = array();
		$list = $this->get_all('*', array(), array('rank' => 'DESC', 'id' => 'ASC'));
		if(!empty($list['list'])){
			foreach($list['list'] as $plugin){
				$plugin_all[$plugin['mark']] = $plugin;
				if($plugin['state'] == 1){
					$plugins[$plugin['mark']] = $plugin;
				}
			}
		}
		cache_write('plugin_all.php', $plugin_all);
		//cache_write('plugins.php', $plugins);
		return true;
	}
	
}
?>